package com.example.administrator.hqq.activity.dating;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.alertview.AlertViewFactory;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.bean.Complain;
import com.example.administrator.hqq.bean.dating.DatingDetailsBean;
import com.example.administrator.hqq.bean.event.DatingEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.config.UrlFactory;
import com.example.administrator.hqq.utils.HintWordsUtils;
import com.google.gson.Gson;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;
import okhttp3.Call;

public class DatingDetails2Activity extends MyActivity {

    @BindView(R.id.return_auto)
    AutoRelativeLayout returnAuto;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.report_auto)
    AutoRelativeLayout reportAuto;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.issue)
    TextView issue;
    @BindView(R.id.pic)
    ImageView pic;
    @BindView(R.id.introduce)
    TextView introduce;
    @BindView(R.id.chat)
    Button chat;
    private String TAG = this.getClass().getSimpleName();
    private DatingDetailsBean datingDetailsBean;
    private AlertDialog showReportAlertView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dating_details2);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initview();
        initdata();
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }

    private void initview() {
        title.setText(getIntent().getStringExtra(LoongsConfig.title) + "的相亲贴");
        reportAuto.setVisibility(View.VISIBLE);
    }

    private void initdata() {
        OkHttpUtils.get().url(UrlFactory.getDatingDetail())
                .addParams(LoongsConfig.user_id, LoongsConfig.getUser_id())
                .addParams(LoongsConfig.dating_id, getIntent().getStringExtra(LoongsConfig.dating_id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "详情角详情onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "详情角详情onResponse: " + response);
                Gson gson = new Gson();
                datingDetailsBean = gson.fromJson(response, DatingDetailsBean.class);
                if (datingDetailsBean.getCode() == 200) {
                    if (!TextUtils.isEmpty(datingDetailsBean.getDating_id().getUrl()))
                        Glide.with(DatingDetails2Activity.this)
                                .load(datingDetailsBean.getDating_id().getUrl())
                                .fitCenter().into(pic);
                    introduce.setText(datingDetailsBean.getDating_id().getText());
                }
            }
        });
    }

    @OnClick({R.id.return_auto, R.id.chat,R.id.report_auto})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_auto:
                finish();
                break;
            case R.id.chat:
                addDirectchat();
                break;
            case R.id.report_auto:
                List<String> strings = new ArrayList<>();
                strings.clear();
                for (String s : this.getResources().getStringArray(R.array.forum_report)) {
                    strings.add(s);
                }
                showReportAlertView = AlertViewFactory.getInstance().getShowReportAlertView(this, strings, new AlertViewFactory.ReportOnClickListener() {
                    @Override
                    public void submit(int position) {
                        Log.e(TAG, "submit: " + position);
                        showReportAlertView.cancel();
                        reportForum(position);
                    }
                });
                break;
        }
    }

    /**
     * 举报相亲贴
     * @param position
     */
    private void reportForum(int position) {
        OkHttpUtils.get().url(UrlFactory.ReportForum())
                .addParams(LoongsConfig.id, getIntent().getStringExtra(LoongsConfig.dating_id))
                .addParams(LoongsConfig.type, "DATING")
                .addParams(LoongsConfig.user_id, SPUtils.getInstance().getString(LoongsConfig.user_id))
                .addParams(LoongsConfig.reason_id, position + 501 + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "举报相亲角onError: " + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "举报相亲角response: " + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt(LoongsConfig.code);
                    HintWordsUtils.showMarkeWords(jsonObject.getInt(LoongsConfig.code), jsonObject.getString(LoongsConfig.description));
                    if (code == 200) {
                        ToastUtils.showShort("已提交举报");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        DatingEvent datingEvent = new DatingEvent(DatingEvent.DatingType.DELETE);
        datingEvent.setDating_id(getIntent().getStringExtra(LoongsConfig.dating_id));
        EventBus.getDefault().post(datingEvent);
        finish();
    }

    /**
     * 创建私聊
     */
    private void addDirectchat() {
        if (datingDetailsBean!=null&&datingDetailsBean.getDating_id()!=null)
        OkHttpUtils.get().url(UrlFactory.addDirectchat())
                .addHeader("Connection", "close")
                .addParams(LoongsConfig.user_id,LoongsConfig.getUser_id())
                .addParams(LoongsConfig.other_user_id,datingDetailsBean.getDating_id().getOther_user_id())
                .addParams(LoongsConfig.type,"DATING")
                .addParams(LoongsConfig.id,"")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, "开启私聊onError: "+e.getMessage() );
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, "开启私聊onResponse: "+response );
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int code = jsonObject.getInt(LoongsConfig.code);
                    String description = jsonObject.getString(LoongsConfig.description);
                    HintWordsUtils.showMarkeWords(code,description);
                    if (code==200){
                        LoongsConfig.getUsersIdSPUtils().put( LoongsConfig.Conversation.ConversationID,datingDetailsBean.getDating_id().getOther_user_id());
                        SPUtils.getInstance(SPUtils.getInstance().getString(LoongsConfig.user_id)).put(LoongsConfig.Conversation.ChatConversationType, LoongsConfig.Conversation.PRIVATE);
                        RongIM.getInstance().startConversation(DatingDetails2Activity.this, Conversation.ConversationType.PRIVATE,datingDetailsBean.getDating_id().getOther_user_id(),"");
                        Complain complain = new Complain(Complain.Type.DIRECTCHAT, "");
                        EventBus.getDefault().post(complain);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(String s){

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
